package com.mc.moring.activitymanager;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;

import java.lang.ref.WeakReference;
import java.util.Iterator;
import java.util.Stack;

public class MAFAppActivityManager {
    private Stack<Activity> activityStack;
    private static MAFAppActivityManager instance;

    public static MAFAppActivityManager getInstance() {
        if (instance == null) {
            synchronized (MAFAppActivityManager.class) {
                instance = new MAFAppActivityManager();
            }
        }
        return instance;
    }

    private MAFAppActivityManager() {
    }


    public void addActivity(Activity activity) {
        if (activityStack == null) {
            activityStack = new Stack();
        }
        activityStack.add(activity);
    }


    public WeakReference<Activity> currentActivity() {
        WeakReference<Activity> activityWeakReference = null;
        if (activityStack != null) {
            for (int i = activityStack.size() - 1; i >= 0; i--) {
                if (!activityStack.get(i).isFinishing()){
                    activityWeakReference = new WeakReference<Activity>(activityStack.get(i));
                    break;
                }
            }
            return activityWeakReference;
        }
        return null;
    }


    public void finishActivity() {
        if (activityStack != null) {
            Activity activity = (Activity) activityStack.lastElement();
            this.finishActivity(activity);
        }
    }

    public void finishActivity(Activity activity) {
        if (activity != null && activityStack != null) {
            activityStack.remove(activity);
            if (!activity.isFinishing()) {
                activity.finish();
            }
        }
    }

    public void finishActivity(Class<?> cls) {
        if (activityStack!=null){
            Iterator var2 = activityStack.iterator();
            while (var2.hasNext()) {
                Activity activity = (Activity) var2.next();
                if (activity.getClass().equals(cls)) {
                    this.finishActivity(activity);
                }
            }
        }
    }

    public void finishAllActivity() {
        if (activityStack != null) {
            for (int i = 0; i < activityStack.size(); i++) {
                if (activityStack.get(i) != null && !activityStack.get(i).isFinishing()) {
                    activityStack.get(i).finish();
                }
            }
            activityStack.clear();
        }
    }


    public WeakReference<Activity> getActivity(Class<?> cls) {
        Activity activity = null;
        if (activityStack != null) {
            for (int i = 0; i < activityStack.size(); i++) {
                if (cls.equals(activityStack.get(i).getClass())) {
                    activity = activityStack.get(i);
                    break;
                }
            }
        }
        WeakReference<Activity> weakReference = null;
        if (activity != null) {
            weakReference = new WeakReference<Activity>(activity);
            return weakReference;
        }
        return null;
    }

    public void removeActivity(Activity activity) {
        if (activity != null && activityStack != null) {
            activityStack.remove(activity);
        }
    }

    public Stack<Activity> getActivityStack() {
        if (activityStack == null){
            activityStack =  new Stack();
        }
        return activityStack;
    }

    public void AppExit(Context context) {
        try {
            finishAllActivity();
            ActivityManager activityMgr = (ActivityManager) context
                    .getSystemService(Context.ACTIVITY_SERVICE);
            activityMgr.killBackgroundProcesses(context.getPackageName());
            System.exit(0);
        } catch (Exception e) {
        }
    }
}
